import requests
import re
import json


def get_one_page(url):
    # cookies需要及时更换 猫眼有安全验证
    headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36' ,
               'Cookie': '__mta=50180505.1690438598683.1690445505427.1690445524998.7; uuid_n_v=v1; uuid=2490D0A02C4511EEB17749F97F0F91BDBD5233299C344F4995791E0877BFBBCD; _csrf=3f852535806998f0c0cef165c6a9971d7e2f2b77b5d517c4c2ffa5508333e6f5; _lxsdk_cuid=18995fdfaafc8-0c8141a2e183cb-d505429-e1000-18995fdfaafc8; _lxsdk=2490D0A02C4511EEB17749F97F0F91BDBD5233299C344F4995791E0877BFBBCD; Hm_lvt_703e94591e87be68cc8da0da7cbd0be2=1690438598; Hm_lpvt_703e94591e87be68cc8da0da7cbd0be2=1690445524; _lxsdk_s=18996675a6f-d8f-ba8-e26%7C%7C5'}

    resp=requests.get(url,headers=headers)
    # print(resp.text)
    if resp.status_code==200:
        return resp.text
    return None

def parse_one_page(text,ptr):
    pattern=re.compile(ptr,re.S)
    items= re.findall(pattern,text)
    # print(items)
    for item in items:
         yield {'index': item[0],
            'image': item[1],
            'title': item[2].strip(),
            'actor': item[3].strip()[3:] if len(item[3]) > 3 else '',
            'time': item[4].strip()[5:] if len(item[4]) > 5 else '',
            'score': item[5].strip() + item[6].strip()}
         
def wirte_to_file(content):
    with open('maoyan.txt', 'a',encoding='utf-8') as wirte_file:
        wirte_file.write(json.dumps(content,ensure_ascii=False)+'\n')

def main(offset):
    url='http://maoyan.com/board/4?offset='+str(offset)
    html=get_one_page(url)
    # print(html)

    # pattern= '<dd>.*?board-index.*?>(.*?)</i>'
    # pattern= '<dd>.*?board-index.*?>(.*?)</i>.*?data-src="(.*?)"'
    # pattern= '<dd>.*?board-index.*?>(.*?)</i>.*?data-src="(.*?)".*?name.*?a.*?>(.*?)</a>'
    pattern= '<dd>.*?board-index.*?>(.*?)</i>.*?data-src="(.*?)".*?name.*?a.*?>(.*?)</a>.*?star.*?>(.*?)</p>.*?releasetime.*?>(.*?)</p>.*?integer.*?>(.*?)</i>.*?fraction.*?>(.*?)</i>.*?</dd>'
    
    for val in parse_one_page(html,pattern):
        print(val)
        wirte_to_file(val)

# main()

# 
if __name__ == '__main__':
    for i in range(10):
        main(i*10)